/*********************************************************************
	filename:	DaterEventMessageCreator.cpp
	created:	6/5/2012
	author:		4Ys
	
	purpose:	Implement the EventMessageCeator class.
*********************************************************************/

#include "DaterEventMessageCreator.h"
#include "DaterCommon.h"
#include "DaterGroupEvent.h"

#include <strstream>

namespace Dater
{

	std::string EventMessageCreator::getAddEventMessage(Event* _event)
	{
		std::string re, evtID, uid;
		Reminding* reminding = _event->getReminding();
		Repeating* repeating = _event->getRepeating();
		std::strstream str;

		str.clear();

		str << _event->getEventID();
		str >> evtID;
		str.clear();
		str << _event->getUserID();
		str >> uid;
		str.clear();

		re = "0\b0\b" + uid+ "\b" +evtID+ "\b"+ _event->getName()
			+ "\b" + timeToString(_event->getCreateTime()) + "\b" + timeToString(_event->getStartTime())
			+ "\b" + timeToString(_event->getFinishTime()) + "\b" +_event->getDescription() + "\b" + _event->getType();

		if(_event->getType() == "GroupEvent")
		{
			std::string groupEventID_s;
			str << static_cast<GroupEvent*>(_event)->getGroupEventID();
			str >> groupEventID_s;
			re += "\b" + groupEventID_s;
		}

		if(reminding == NULL)
		{
			re += "\b0";
		}else
		{
			re += "\b1";
			std::string remindTime;
			str << reminding->getRemindTime();
			str >> remindTime;
			re += "\b" + remindTime + "\b" + reminding->getRemindUnit();
		}

		if(repeating == NULL)
		{
			re += "\b0";
		}else
		{
			re += "\b1";
			std::string repeatInverval;
			str << repeating->getRepeatInterval();
			str >> repeatInverval;
			re += "\b" + repeatInverval + "\b" + repeating->getRepeatUnit() + "\b" +
				timeToString(repeating->getRepeatEndTime());
		}

		return re;
	}

	std::string EventMessageCreator::getDeleteEventMessage(Event* _event)
	{
		std::string re, evtID, uid;
		std::strstream str;

		str << _event->getEventID();
		str >> evtID;
		str.clear();
		str << _event->getUserID();
		str >> uid;

		re = "0\b1\b" + uid + "\b" + evtID;
		const char* re_c = re.c_str();
		return re_c;
	}

	std::string EventMessageCreator::getUpdateEventMessage(Event* _event)
	{
		std::string re, evtID, uid;;
		Reminding* reminding = _event->getReminding();
		Repeating* repeating = _event->getRepeating();
		std::strstream str;

		str << _event->getEventID();
		str >> evtID;
		str.clear();
		str << _event->getUserID();
		str >> uid;

		re = "0\b2\b" + uid+ "\b" +evtID+ "\b "+ _event->getName()
			+ "\b" + timeToString(_event->getCreateTime()) + "\b" + timeToString(_event->getStartTime())
			+ "\b" + timeToString(_event->getFinishTime()) + "\b" +_event->getDescription();
		if(reminding == NULL)
		{
			re += "\b0";
		}else
		{
			re += "\b1";
			std::string remindTime;
			str << reminding->getRemindTime();
			str >> remindTime;
			re += "\b" + remindTime + "\b" + reminding->getRemindUnit();
		}

		if(repeating == NULL)
		{
			re += "\b0";
		}else
		{
			re += "\b1";
			std::string repeatInverval;
			str << repeating->getRepeatInterval();
			str >> repeatInverval;
			re += "\b" + repeatInverval + "\b" + repeating->getRepeatUnit() + "\b" +
				timeToString(repeating->getRepeatEndTime());
		}

		return re;
	}

	std::string EventMessageCreator::getViewEventMessage(int userID)
	{
		std::string re, uid;
		std::strstream str;

		str << userID;
		str >> uid;

		re = "0\b3\b" +uid;

		return re;
	}

}